﻿import mx.utils.Delegate;
import arkadium.SoundManager;

class arkadium.SoundClip
{
	private var _sSoundName:String;
	public var _snSound:Sound;
	private var _bMusic:Boolean;
	private var _bIsPlaying:Boolean;
	private var _nPauseTime:Number;
	private var _nVolume:Number;
	private var _nLoopNumber:Number;
	private var _cMySoundManager:SoundManager;
	
	private var _mcTarget:MovieClip;
	
	public function SoundClip(cMySoundManager:SoundManager)
	{
		_cMySoundManager = cMySoundManager
		_snSound = null;
	}
	
	public function attachSoundInLibrary( sLinkageID:String, mcTarget:MovieClip, bMusic:Boolean, nVolume:Number )
	{
		_sSoundName = sLinkageID;
		_bMusic = bMusic;
		_mcTarget = mcTarget;
		
		_nPauseTime = 0;
		
		_snSound = new Sound( mcTarget );
		_snSound.attachSound( sLinkageID );
		
		setVol(nVolume);
		_snSound.onSoundComplete = Delegate.create( this, soundComplete );
		
		//sounds can be called directly through these dymnamic play functions
		_cMySoundManager[sLinkageID+"Start"] = Delegate.create(this, playSound);
		_cMySoundManager[sLinkageID+"Stop"] = Delegate.create(this, stopSound);
		
	}
	
	public function attachExternalSound( sLinkageID:String, mcTarget:MovieClip, sURL:String, bMusic:Boolean, nVolume:Number )
	{
		_sSoundName = sLinkageID;
		_bMusic = bMusic;
		_mcTarget = mcTarget;
		
		_nPauseTime = 0;
		_nVolume = nVolume;
		_snSound = new Sound( mcTarget );
		//_snSound.attachSound( sLinkageID );
		_snSound.onLoad = Delegate.create( this, soundLoaded );
		_snSound.loadSound(sURL, false);

		_snSound.onSoundComplete = Delegate.create( this, soundComplete );
		
		
	}
	
	public function soundLoaded() {
		trace("COMPLETED");
		setVol(_nVolume);
		_snSound.onSoundComplete = Delegate.create( this, soundComplete );
		
		//sounds can be called directly through these dymnamic play functions
		_cMySoundManager[_sSoundName+"Start"] = Delegate.create(this, playSound);
		_cMySoundManager[_sSoundName+"Stop"] = Delegate.create(this, stopSound);
		
	
		if(_cMySoundManager.getSoundState() == false && _bMusic == false){
			var oSoundLeft:Object = { ll: 0, lr: 0, rr: 0, rl: 0};
			_snSound.setTransform(oSoundLeft);
		}
		else if(_cMySoundManager.getMusicState() == false && _bMusic == true){
			var oSoundLeft:Object = { ll: 0, lr: 0, rr: 0, rl: 0};
			_snSound.setTransform(oSoundLeft);
		}
	}
	
	public function setVol( nVolume:Number){
		_snSound.setVolume(nVolume);
		_snSound.getVolume();
		_nVolume = nVolume;
	}
	
	public function getSoundName():String
	{
		return _sSoundName;
	}
	
	public function playSound(nLoopNumber:Number)
	{
		if(nLoopNumber==undefined)_nLoopNumber = 0;
		else _nLoopNumber = nLoopNumber;
		//trace( "PLAY SOUND: " + _sSoundName ) ;		
		_snSound.start( _nPauseTime, 0 );
		_bIsPlaying = true;
	}
	
	private function soundComplete()
	{
		_bIsPlaying = false;
		_nPauseTime = 0;
		_nLoopNumber--;
		if ( _bMusic || _nLoopNumber>0)
		{
			playSound();
		}
	}
	
	public function stopSound()
	{
		//trace( "STOP SOUND: " + _sSoundName ) ;
		_bIsPlaying = false;
		_snSound.stop();
	}
	
	
	public function pauseSound()
	{
		//trace( "PAUSE SOUND: " + _sSoundName ) ;
		_nPauseTime = _snSound.position / 1000;
		stopSound();
	}
	
	public function setVolume( nVolume:Number )
	{
		if ( nVolume > -1 && nVolume < 101 )
		{
			_snSound.setVolume( nVolume );

		}
	}

	
	/*
	TODO: features
	-pause/unpause functionality with EventManager.
	-MP3 loading
	-pause of looping tracks
	-set Volume
	-mute
	
	
	*/
	
	
	
	
	
}